package com.dongdong.bookstore.module.ams.controller;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongdong.bookstore.common.api.CommonPage;
import com.dongdong.bookstore.common.api.ResultData;
import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.ams.dto.ArtcleDto;
import com.dongdong.bookstore.module.ams.model.Article;
import com.dongdong.bookstore.module.ams.service.AmsCacheService;
import com.dongdong.bookstore.module.ams.service.ArticleService;
import com.dongdong.bookstore.module.ams.viewmodel.ArticleAddParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * <p>
 * 文章表  前端控制器
 * </p>
 *
 * @author chiangkai
 * @since 2021-10-22
 */
@Api(tags = "文章模块")
@RestController
/*@RequestMapping("/ams/article")*/
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private AmsCacheService amsCacheService;

    @ApiOperation("用户是否为该文章文章点赞")
    @GetMapping("/ams/article/ifLike/{articleId}/{userId}")
    public ResultData<Boolean> ifLike(@PathVariable("articleId") Long articleId,
                                         @PathVariable("userId") Long userId) {


        boolean b = amsCacheService.ifLike(articleId, userId);


        if(!b){
            return ResultData.result(200,"还未点过赞",false);
        }
        return ResultData.result(200,"已经给本文点过赞了",true);

    }


    @ApiOperation("文章点赞与取消点赞")
    @GetMapping("/ams/article/judgeLike/{articleId}/{userId}")
    public ResultData<Boolean> judgeLike(@PathVariable("articleId") Long articleId,
                                         @PathVariable("userId") Long userId) {

        boolean isLike = articleService.judgeLike(articleId, userId);
        if (!isLike) {
            return ResultData.ok("取消点赞");
        }
        return ResultData.ok("点赞成功");
    }

    @ApiOperation("添加文章")
    @PostMapping("/ams/article/addArticle")
    public ResultData<Integer> addArticle(@RequestBody ArticleAddParam param) {
        Integer integer = articleService.addArticle(param);
        if (integer == null) {
            return ResultData.fail("添加失败");
        }
        return ResultData.ok("添加成功", integer);
    }

    @ApiOperation("更新文章")
    @PostMapping("/ams/article/updateArticle/{id}")
    public String updateArticle(@PathVariable Long id, @RequestBody ArticleAddParam articleAddParam) {
        boolean success = articleService.updateArticle(id, articleAddParam);
        if (success) {
            return "更新成功";
        } else {
            Asserts.fail("更新失败");
        }
        return null;
    }

    @ApiOperation("删除文章")
    @GetMapping("/ams/article/deleteArticle/{id}")
    public String deleteArticle(@PathVariable Long id) {
        boolean success = articleService.deleteArticle(id);
        if (success) {
            return "删除成功";
        } else {
            Asserts.fail("删除失败");
        }
        return null;
    }


    @ApiOperation("获取文章列表并分页")
    @GetMapping("/p/ams/article/articleList")
    public CommonPage<ArtcleDto> getArticleList(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
    ) {
        Page<Article> articleList = articleService.getArticleListByPage(keyword, pageSize, pageNum);

        ArrayList<ArtcleDto> list = new ArrayList<>();
        articleList.getRecords().forEach(article -> {
            ArtcleDto dto = new ArtcleDto();
            BeanUtils.copyProperties(article, dto);
            dto.setView(amsCacheService.getViews(dto.getId()));
            dto.setLike(amsCacheService.getLike(dto.getId()));
            list.add(dto);
        });
        return CommonPage.restPage(Convert.toInt(articleList.getCurrent()),
                Convert.toInt(articleList.getSize()),
                articleList.getTotal(),
                list
        );
        //return CommonPage.restPage(articleList);
    }

    @ApiOperation("获取文章详情页")
    @GetMapping("/p/ams/article/detail/{id}")
    public ResultData<Article> getArticleDetail(@PathVariable("id") long id) {
        Article articleById = articleService.getArticleById(id);

        return ResultData.ok("获取id：" + id + "文章成功 ", articleById);
    }
}
